<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="订单编号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付方式" prop="paymentMethod">
        <el-select
          v-model="queryParams.paymentMethod"
          placeholder="请选择支付方式"
          clearable
        >
          <el-option
            v-for="dict in payment_method"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付状态" prop="paymentStatus">
        <el-select
          v-model="queryParams.paymentStatus"
          placeholder="请选择支付状态"
          clearable
        >
          <el-option
            v-for="dict in payment_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        label="第三方支付订单号"
        label-width="150px"
        prop="transactionId"
      >
        <el-input
          v-model="queryParams.transactionId"
          placeholder="请输入第三方支付订单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="退款状态" prop="refundStatus">
        <el-select v-model="queryParams.refundStatus" placeholder="请选择退款状态" clearable>
          <el-option v-for="dict in refund_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="支付账号" prop="payerId">
        <el-input v-model="queryParams.payerId" placeholder="请输入支付账号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="支付设备信息" prop="deviceInfo">
        <el-input v-model="queryParams.deviceInfo" placeholder="请输入支付设备信息" clearable @keyup.enter="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="支付成功时间" prop="paymentTime">
        <el-date-picker clearable v-model="queryParams.paymentTime" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择支付成功时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="退款成功时间" prop="refundTime">
        <el-date-picker clearable v-model="queryParams.refundTime" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择退款成功时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option
            v-for="dict in del_flag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 按钮操作 -->
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['ruoyi:payLogs:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['ruoyi:payLogs:edit']">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ruoyi:payLogs:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ruoyi:payLogs:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表单 -->
    <el-table
      v-loading="loading"
      :data="payLogsList"
      border
      resizable
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="序号"
        type="index"
        width="60"
        align="center"
        prop="paymentId"
      />
      <el-table-column
        label="订单编号"
        align="center"
        min-width="150"
        prop="orderId"
      />
      <el-table-column
        label="支付方式"
        align="center"
        min-width="150"
        prop="paymentMethod"
      >
        <template #default="scope">
          <dict-tag
            :options="payment_method"
            :value="scope.row.paymentMethod"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="支付状态"
        align="center"
        min-width="150"
        prop="paymentStatus"
      >
        <template #default="scope">
          <dict-tag
            :options="payment_status"
            :value="scope.row.paymentStatus"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column label="第三方支付订单号" align="center" min-width="150" prop="transactionId" /> -->
      <!-- <el-table-column label="退款状态" align="center" min-width="150" prop="refundStatus">
        <template #default="scope">
          <dict-tag :options="refund_status" :value="scope.row.refundStatus" />
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="支付金额" align="center" min-width="150" prop="amount" /> -->

      <el-table-column
        prop="amount"
        align="center"
        min-width="150"
        label="支付金额"
      >
        <template #default="scope">
          {{ formatAmount(scope.row.amount) }}
        </template>
      </el-table-column>

      <!-- <el-table-column label="退款金额" align="center" min-width="150" prop="refundAmount" /> -->

      <el-table-column
        prop="refundAmount"
        align="center"
        min-width="150"
        label="退款金额"
      >
        <template #default="scope">
          {{ formatAmount(scope.row.refundAmount) }}
        </template>
      </el-table-column>

      <!-- <el-table-column label="支付账号" align="center" min-width="150" prop="payerId" /> -->
      <!-- <el-table-column label="支付设备信息" align="center" min-width="150" prop="deviceInfo" /> -->
      <el-table-column
        label="支付成功时间"
        align="center"
        min-width="150"
        prop="paymentTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.paymentTime, "{y}-{m}-{d} {h}:{i}:{s}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="退款成功时间"
        align="center"
        min-width="150"
        prop="refundTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.refundTime, "{y}-{m}-{d} {h}:{i}:{s}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="del_flag" :value="scope.row.status" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="备注信息" align="center" min-width="150" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        min-width="240"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <!-- <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['ruoyi:payLogs:edit']">修改</el-button> -->
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ruoyi:payLogs:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改支付记录信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="payLogsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单ID" />
        </el-form-item>
        <el-form-item label="支付方式" prop="paymentMethod">
          <el-select v-model="form.paymentMethod" placeholder="请选择支付方式">
            <el-option
              v-for="dict in payment_method"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付状态" prop="paymentStatus">
          <el-select v-model="form.paymentStatus" placeholder="请选择支付状态">
            <el-option
              v-for="dict in payment_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="第三方支付订单号" prop="transactionId">
          <el-input
            v-model="form.transactionId"
            placeholder="请输入第三方支付订单号"
          />
        </el-form-item>
        <el-form-item label="退款状态" prop="refundStatus">
          <el-select v-model="form.refundStatus" placeholder="请选择退款状态">
            <el-option
              v-for="dict in refund_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入支付金额" />
        </el-form-item>
        <el-form-item label="退款金额" prop="refundAmount">
          <el-input v-model="form.refundAmount" placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="支付账号" prop="payerId">
          <el-input v-model="form.payerId" placeholder="请输入支付账号" />
        </el-form-item>
        <el-form-item label="支付设备信息" prop="deviceInfo">
          <el-input
            v-model="form.deviceInfo"
            placeholder="请输入支付设备信息"
          />
        </el-form-item>
        <el-form-item label="支付成功时间" prop="paymentTime">
          <el-date-picker
            clearable
            v-model="form.paymentTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择支付成功时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退款成功时间" prop="refundTime">
          <el-date-picker
            clearable
            v-model="form.refundTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择退款成功时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in del_flag"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="删除" prop="delFlag">
          <el-radio-group v-model="form.delFlag">
            <el-radio
              v-for="dict in wdm_status"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PayLogs">
import {
  listPayLogs,
  getPayLogs,
  delPayLogs,
  addPayLogs,
  updatePayLogs,
} from "@/api/ruoyi/payLogs";

const { proxy } = getCurrentInstance();
const { refund_status, wdm_status, payment_method, payment_status, del_flag } =
  proxy.useDict(
    "refund_status",
    "wdm_status",
    "payment_method",
    "payment_status",
    "del_flag"
  );

const payLogsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderId: null,
    paymentMethod: null,
    paymentStatus: null,
    transactionId: null,
    refundStatus: null,
    payerId: null,
    deviceInfo: null,
    paymentTime: null,
    refundTime: null,
    status: null,
  },
  rules: {
    orderId: [{ required: true, message: "订单ID不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询支付记录信息列表 */
function getList() {
  loading.value = true;
  listPayLogs(queryParams.value).then((response) => {
    // payLogsList.value = response.rows
    // 过滤已删除数据（假设 delFlag 字段标识删除状态）
    payLogsList.value = response.rows.filter((item) => item.delFlag !== "2");
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    paymentId: null,
    orderId: null,
    paymentMethod: null,
    paymentStatus: null,
    transactionId: null,
    refundStatus: null,
    amount: null,
    refundAmount: null,
    payerId: null,
    deviceInfo: null,
    paymentTime: null,
    refundTime: null,
    status: null,
    delFlag: null,
    version: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null,
  };
  proxy.resetForm("payLogsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.paymentId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加支付记录信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _paymentId = row.paymentId || ids.value;
  getPayLogs(_paymentId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改支付记录信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["payLogsRef"].validate((valid) => {
    if (valid) {
      if (form.value.paymentId != null) {
        updatePayLogs(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPayLogs(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _paymentIds = row.paymentId || ids.value;
  proxy.$modal
    .confirm('是否确认删除支付记录信息编号为"' + _paymentIds + '"的数据项？')
    .then(function () {
      return delPayLogs(_paymentIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "ruoyi/payLogs/export",
    {
      ...queryParams.value,
    },
    `payLogs_${new Date().getTime()}.xlsx`
  );
}

// 格式化金额（分转元）
function formatAmount(value) {
  if (!value && value !== 0) return "--";
  return (value / 100).toFixed(2);
}

getList();
</script>
